//
//  GWBaseToolBar.m
//  新浪微博
//
//  Created by qianfeng on 16/2/25.
//  Copyright (c) 2016年 高炜. All rights reserved.
//

#import "GWBaseToolBar.h"
#import "GWStatuseModel.h"
@interface GWBaseToolBar ()
@property (nonatomic,strong)NSMutableArray *btnArray;

//转发
@property (nonatomic,weak)UIButton *repostsBtn;
//评论
@property (nonatomic,weak)UIButton *commentsBtn;
//表态
@property (nonatomic,weak)UIButton *attitudesBtn;
@end

@implementation GWBaseToolBar

-(NSMutableArray *)btnArray
{
    if (_btnArray==nil) {
        _btnArray =[NSMutableArray array];
    }
    return _btnArray;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
        
        self.userInteractionEnabled =YES;
        self.backgroundColor =[UIColor clearColor];
        self.repostsBtn =[self setupBtnWithIcon:@"timeline_icon_retweet" title:@"转发"];
        self.commentsBtn =[self setupBtnWithIcon:@"timeline_icon_comment" title:@"评论"];
        self.attitudesBtn =[self setupBtnWithIcon:@"timeline_icon_unlike" title:@"赞"];
        
    }
    return self;
}
-(UIButton *)setupBtnWithIcon:(NSString *)icon title:(NSString *)title
{
    UIButton *btn =[[UIButton alloc]init];
    [btn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    btn.titleLabel.font =[UIFont systemFontOfSize:13];
    
    [btn setBackgroundImage:[UIImage reSizeWithName:@"common_card_bottom_background_highlighted"] forState:UIControlStateHighlighted];
    btn.adjustsImageWhenHighlighted =NO;
    
    btn.titleEdgeInsets =UIEdgeInsetsMake(0, 10, 0, 0);
    [self.btnArray addObject:btn];
    [self addSubview:btn];
    return btn;
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    NSInteger btnCount =self.btnArray.count;
    CGFloat btnW =self.width/btnCount;
    CGFloat btnH =self.height;
    for (int i =0; i<btnCount; i++) {
        UIButton *btn =self.btnArray[i];
        btn.frame =CGRectMake(btnW*i, 0, btnW, btnH);
    }
  
}

-(void)setStatuse:(GWStatuseModel *)statuse
{
    _statuse =statuse;
    
    
    [self setUpBtn:self.repostsBtn setTitle:@"转发" WithCount:_statuse.reposts_count];
    [self setUpBtn:self.commentsBtn setTitle:@"评论" WithCount:_statuse.comments_count];
    [self setUpBtn:self.attitudesBtn setTitle:@"赞" WithCount:_statuse.attitudes_count];
    
}
-(void)setUpBtn:(UIButton *)btn setTitle:(NSString *)title WithCount:(int)count
{
    if (count>=10000) {
        title =[NSString stringWithFormat:@"%.1f万",count/10000.0];
        title =[title stringByReplacingOccurrencesOfString:@".0" withString:@""];
    }
    else if(count>0)
    {
        title =[NSString stringWithFormat:@"%d",count];
    }
    [btn setTitle:title forState:UIControlStateNormal];
}

@end
